package com.solcoo.merchant.ui.pay;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.android.common.util.ToastUtil;
import com.solcoo.merchant.ApplicationExt;
import com.solcoo.merchant.Constant;
import com.solcoo.merchant.R;
import com.solcoo.merchant.base.BaseActivity;
import com.solcoo.merchant.base.task.TaskHelper;
import com.solcoo.merchant.base.task.TaskHelper.Callback;
import com.solcoo.merchant.base.task.TaskHelper.DefaultCallback;
import com.solcoo.merchant.model.PayResult;
import com.solcoo.merchant.util.CheckUtil;
import com.solcoo.merchant.util.CheckUtil.CheckResult;
import com.solcoo.merchant.util.ViewUtil;

public class PayActivity extends BaseActivity {

	EditText moneyInput;

	String money;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pay);
		moneyInput = (EditText) findViewById(R.id.money);
	}

	public void sendOnClick(View v) {
		money = moneyInput.getText().toString();
		CheckResult checkResult = CheckUtil.checkMoney(money);
		if (checkResult.isSuccess()) {
			ViewUtil.startCapture(v.getContext(), 0);
		} else {
			ToastUtil.showToast(this, checkResult.failMsg);
		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			Bundle bundle = data.getExtras();
			String scanResult = bundle.getString("result");
			CheckResult checkResult = CheckUtil.checkPhoneNo(scanResult);
			if (checkResult.isSuccess()) {
				TaskHelper.oper(this, Constant.URL.getPay(
						ApplicationExt.getUser().yonghum, scanResult, money),
						callback, PayResult.class);
			} else {
				ToastUtil.showToast(this, "扫描内容为：" + scanResult);
			}
		}
	}

	private Callback<PayResult> callback = new DefaultCallback<PayResult>(this) {

		@Override
		public void onSuccess(PayResult result) {
			Intent it = new Intent(PayActivity.this, PaySuccessActivity.class);
			it.putExtra(PaySuccessActivity.INTENT_KEK_TITLE, "支付结果");
			it.putExtra(PaySuccessActivity.INTENT_KEK_INFO, result.msg);
			it.putExtra(PaySuccessActivity.INTENT_KEK_MSG, "支付成功");
			startActivity(it);
			finish();
		}

		public void onFail(PayResult result) {
			Intent it = new Intent(PayActivity.this, PaySuccessActivity.class);
			it.putExtra(PaySuccessActivity.INTENT_KEK_TITLE, "支付结果");
			it.putExtra(PaySuccessActivity.INTENT_KEK_INFO, result.msg);
			it.putExtra(PaySuccessActivity.INTENT_KEK_FAIL, result.failMsg);
			startActivity(it);
			finish();
		}
	};
}
